package com.netted.operationassistant.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.mylhyl.superdialog.SuperDialog;
import com.netted.operationassistant.R;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.http.HttpRequest;

import java.util.HashMap;
import java.util.Map;

public class AdminMenuActivity extends BaseActivity {

    private String password;
    private String account;
    private String userId;

    private Button arouseBtn;//激活按钮
    private Button relieveBtn;//解除机柜按钮
    private Button bindBtn;//机柜绑定数据按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//
        setContentView(R.layout.activity_admin_menu);

        arouseBtn = (Button)findViewById(R.id.admin_menu_arouseBtn) ;
        relieveBtn = (Button)findViewById(R.id.admin_menu_relieveBtn) ;
        bindBtn = (Button)findViewById(R.id.admin_menu_bindBtn) ;

        Intent intent = getIntent();
        password = intent.getStringExtra("password");
        account = intent.getStringExtra("account");
        userId = intent.getStringExtra("userId");


        if ("".equals(catbinetId) && "".equals(catbinetId) && "".equals(catbinetId) && "".equals(catbinetId) ){
            arouseBtn.setText("激活机柜");
            arouseBtn.setEnabled(true);
            arouseBtn.setClickable(true);

            relieveBtn.setText("解除机柜(未绑定或激活)");
            relieveBtn.setEnabled(false);
            relieveBtn.setClickable(false);

            bindBtn.setText("绑定已有机柜数据");
            bindBtn.setEnabled(true);
            bindBtn.setClickable(true);
        }else{
            arouseBtn.setText("激活机柜(已激活)");
            arouseBtn.setEnabled(false);
            arouseBtn.setClickable(false);

            relieveBtn.setText("解除机柜");
            relieveBtn.setEnabled(true);
            relieveBtn.setClickable(true);

            bindBtn.setText("绑定已有机柜数据(已绑定)");
            bindBtn.setEnabled(false);
            bindBtn.setClickable(false);
        }
    }

    public void toArouseActivity(View view){//
        Intent intent = new Intent(AdminMenuActivity.this,ArouseActivity.class);
        intent.putExtra("account",account);
        intent.putExtra("password",password);
        intent.putExtra("userId",userId);
        startActivity(intent);
    }
    public void exitActivity(View view){
        Intent intent = new Intent(AdminMenuActivity.this,MenuActivity.class);
        startActivity(intent);
        this.finish();
    }




    public void toUnBindDevice(View view){
        Intent intent = new Intent(this,AdminQrCodeActivity.class);
        intent.putExtra("from","toUnBindDevice");
        startActivity(intent);

    }

    public void toBindDevice(View view){
        Intent intent = new Intent(this,AdminQrCodeActivity.class);
        intent.putExtra("from","toBindDevice");
        startActivity(intent);

    }


    public void toOpenPort(View view){
        Intent intent = new Intent(this,OpenPortActivity.class);
        intent.putExtra("from","toOpenPort");
        startActivity(intent);

    }

    public void relieveCatbinet(View view){
        showNotarizeDialog("提示", "请确定是否清空机柜数据", new SuperDialog.OnClickPositiveListener() {
            @Override
            public void onClick(View v) {
                String url = Constant.HOST + "/ws/opt/relieveCatbinet";
                String catbinetId = sharedPreferencesHelper.getSharedPreference(Constant.XML_CATBINET_ID,"") + "";
                Map<String,String> params = new HashMap<>();
                params.put("catbinetId",catbinetId);
                params.put("userId",userId);
                request.post(url, params, "post", new HttpRequest.Callback() {
                    @Override
                    public void onSuccess(String response) {
                        JSONObject resJosn = JSONObject.parseObject(response);
                        if ("".equals(resJosn.getString("flag"))){

                        }



                        sharedPreferencesHelper.put(Constant.XML_CATBINET_ID,"");
                        sharedPreferencesHelper.put(Constant.XML_CATBINET_CODE,"");
                        sharedPreferencesHelper.put(Constant.XML_CATBINET_NAME,"");
                        sharedPreferencesHelper.put(Constant.XML_SCHOOL_ID,"");
                        sharedPreferencesHelper.put(Constant.XML_CATBINET_PORT_COUNT,"");
                        sharedPreferencesHelper.put(Constant.XML_SCHOOL_NAME,"");
                        sharedPreferencesHelper.put(Constant.XML_SCHOOL_AREA_ID,"");



                        showDialog("提示", "解除绑定成功", new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(AdminMenuActivity.this, AdminMenuActivity.class);
                                startActivity(intent);
                                AdminMenuActivity.this.finish();
                                overridePendingTransition(0,0);
                            }
                        });
                    }

                    @Override
                    public void onError(VolleyError error) {
                        showDialog("提示","接触绑定失败,请联系开发人员",null);
                    }
                });


            }
        }, null);

    }

    public void toBindCatbinetActivity(View view){
        Intent intent = new Intent(this,BindCatbinetActivity.class);
        intent.putExtra("from","AdminMenuActivity");
        startActivity(intent);

    }
}
